<?php
    namespace Npfe\Controller;
    use Think\Controller;
    class NocController extends Controller {
        function Index(){

        }
        function Shownoc(){
            $this->display('djnoc');
        }
        function addnoc(){
            $now_em = session('nickname');
            $addt = array(
                'snid'=> I('post.snid'),
                'cname'=> I('post.cname'),
                'cnumber'=> I('post.cnumber'),
                'overtime'=> I('post.overtime'),
                'isduban'=> I('post.isduban'),
                'fanying'=> I('post.fanying'),
                'huidan'=> I('post.huidan'),
                'gdlb'=> I('post.gdlb'),
                'bz'=> I('post.bz'),
                'time'=> date('Y-m-d H:i:s',time()),
                'xsygs'=> I('post.xsygs'),
                'ctlman'=> $now_em
            );
            try {
                $noc = new \Npfe\Model\NocModel();
                $res = $noc->add($addt);
                if ($res != 0 or $res != false) {
                    echo "系统已成功添加纪录！<a href=\"javascript:void(0)\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-add'\" onclick=\"resetnoc_add()\">返回继续新增工单</a>";
                }
            } catch (Exception $e) {
                if ($e->g=="") {
                    print $e->getMessage();
                }
            }
        }
        function editnoc($snid,$time){
            $now_em = session('nickname');
            $updt = array(
                'snid'=> $snid,
                'cname'=> I('post.cname'),
                'cnumber'=> I('post.cnumber'),
                'overtime'=> I('post.overtime'),
                'isduban'=> I('post.isduban'),
                'fanying'=> I('post.fanying'),
                'huidan'=> I('post.huidan'),
                'gdlb'=> I('post.gdlb'),
                'bz'=> I('post.bz'),
                'xsygs'=> I('post.xsygs'),
                'ctlman'=> $now_em
            );
            try {
                $noc = new \Npfe\Model\NocModel();
                $res = $noc->where("snid = $snid && time = $time")->save($updt);
                if ($res != 0 or $res != false) {
                    echo "系统已成功修改纪录!";
                }else{
                    echo "错误";
                }
            } catch (Exception $e) {
                if ($e->g=="") {
                    print $e->getMessage();
                }
            }
        }
        Public function ViewNocDetails($cn = "",$vtime = ""){
            $noc = new \Npfe\Model\NocModel();
            $cd = array(
                'cnumber' => $cn,
                'time' => $vtime
            );
            $noc->where($cd)->find();
            $data = $noc->data();
            if($data['hhj'] == 'true'){
                $data['hhj'] = $this->CheckHHJ($cn);
            }
            //dump($data);
            $this->assign($data);
            $this->display();

        }
        private function CheckHHJ($gd_cn = ''){
            $pg = new \Npfe\Model\PgModel();
            $cd = array(
                'gd_cn' => $gd_cn
            );
            $data = $pg->where($cd)->select();
            $xml = '';
            foreach($data as $value){
                $xml .= '<tr><td>'.$value['pg_name'].'</td><td>'.$value['pg_begintime'].'</td><td>'.$value['pg_rechtime'].'</td><td>'.$value['pg_yjbx'].'</td><td>'.$value['pg_sjbx'].'</td><td>'.$value['traceman'].'</td><td>'.$value['oman'].'</td><td>'.$value['bz'].'</td><td>'.$value['bh'].'</td><td>'.$value['pg_name'].'</td></tr>';
            }

            return $xml;
        }
        public function listnoc(){
            $posti = $_POST;
            $noc = new \Npfe\Model\NocModel();
            $items = array();
            $dmt = \Npfe\Controller\MainController::getdmt();
            $sql = "time >= '$dmt[dmtb]' and time <= '$dmt[dmte]'";
            if (!empty($posti['lxnumber'])) {
                $sql = "cnumber = '$posti[lxnumber]'";
            }elseif (!empty($posti)){
                $formated = \Npfe\Controller\MainController::formattime($posti['starttime'], $posti['endtime']);
                $sql = "time >= '$formated[stt]' && time <= '$formated[ett]'";
            }
            $data = $noc->where($sql)->select();
            foreach($data as $i => $row){
                $row['hhj']=$row['hhj']=="true"?"是":"否";
                array_push($items, $row);
            }
            $value =array(
                "total" => count($items),
                'rows' => $items
            );
            $nonetr = json_encode($value);
            $resjson = $nonetr;
            echo $nonetr;
        }
}